package com.rd.student.jike.class2.lihongming.week731_806.Day0804;

import java.util.concurrent.ExecutionException;

/**
 * @Auther:Heinrich
 * @Date:2023/8/4-08-04-10:05
 * @Description:com.rd.student.jike.class2.lihongming.Day0804
 * @version:1.0
 */
public class Cross implements Runnable {
    //AtomicInteger temp = new AtomicInteger(1);
    Integer temp = 1;
    @Override
    public void run() {
        int count = temp;
        synchronized (this){
            while(temp != 1 && temp==count){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + " " + temp++);
            this.notifyAll();
            if(Integer.parseInt(String.valueOf(temp))==11) System.out.println("所有人都通过了");
        }
    }
}
class Test{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Cross cross = new Cross();
        new Thread(cross, "贾复").start();
        new Thread(cross, "贾似道").start();
        new Thread(cross, "史弥远").start();
        new Thread(cross, "张俊").start();
        new Thread(cross, "刘光世").start();
        new Thread(cross, "韩世忠").start();
        new Thread(cross, "王阳明").start();
        new Thread(cross, "马周").start();
        new Thread(cross, "姚崇").start();
        new Thread(cross, "宋璟").start();
    }
}
